using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CollisionDetectionSandbox
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class ZeusGame : Microsoft.Xna.Framework.Game
    {
        TitleScreen mTitleScreen;
        Screen mCurrentScreen;
        Screen mNextScreen;
        GameScreen mGameScreen;
        HowToScreen mHowToScreen;
        ScoreScreen mScoreScreen;
        CreditsScreen mCreditsScreen;
        SplashScreen mDeathSplashScreen;
        SplashScreen mVictorySplashScreen;
        MouseState mouse_state;
        public static int xpos = GameScreen.GAME_WIDTH / 2 - Button.BUTTON_WIDTH / 2;
        public static int ypos = GameScreen.GAME_HEIGHT/ 2 - Button.BUTTON_HEIGHT / 2;
        GraphicsDeviceManager m_graphics;
        SpriteBatch m_spriteBatch;
        Texture2D CrosshairTexture;
        public static readonly int GAME_WIDTH = 800;
        public static readonly int GAME_HEIGHT = 600;
        public static readonly int HUD_HEIGHT = 50;
        public static bool outcome = false;
        public static int score = 0;

        public ZeusGame()
        {
            m_graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            m_graphics.PreferredBackBufferHeight = GAME_HEIGHT;
            m_graphics.PreferredBackBufferWidth = GAME_WIDTH;
            
            //m_graphics.IsFullScreen = true;
           // m_graphics.ToggleFullScreen();                                         //THIS WORKS BETTER FOR FULLSCREEN

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            m_spriteBatch = new SpriteBatch(GraphicsDevice);
            mTitleScreen = new TitleScreen(this.Content, new EventHandler(TitleScreenEvent));
            mHowToScreen = new HowToScreen(this.Content, new EventHandler(HowToScreenEvent));
            mGameScreen = new GameScreen(this.Content, new EventHandler(GameScreenEvent));
            mScoreScreen = new ScoreScreen(this.Content, new EventHandler(ScoreScreenEvent));
            mCreditsScreen = new CreditsScreen(this.Content, new EventHandler(CreditsScreenEvent));
            mDeathSplashScreen = new SplashScreen(this.Content, new EventHandler(SplashScreenEvent), "Main Textures/DeathSplashScreen");
            mVictorySplashScreen = new SplashScreen(this.Content, new EventHandler(SplashScreenEvent), "Main Textures/VictorySplashScreen");
            mCurrentScreen = mTitleScreen;
            // TODO: use this.Content to load your game content here
            CrosshairTexture = Content.Load<Texture2D>("Main Textures/CrossHair2");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        
        
        protected override void Update(GameTime gameTime)
        {
            if (mCurrentScreen == mTitleScreen)
            {
                mTitleScreen.Update(gameTime);
            }
            else if (mCurrentScreen == mHowToScreen)
            {
                mHowToScreen.Update(gameTime);
            }
            else if (mCurrentScreen == mGameScreen)
            {
                mGameScreen.Update(gameTime);
            }
            else if (mCurrentScreen == mScoreScreen)
            {
                mScoreScreen.Update(gameTime);
            }
            else if (mCurrentScreen == mCreditsScreen)
            {
                mCreditsScreen.Update(gameTime);
            }
            else if (mCurrentScreen == mDeathSplashScreen)
            {
                mDeathSplashScreen.Update(gameTime);
            }
            else if (mCurrentScreen == mVictorySplashScreen)
            {
                mVictorySplashScreen.Update(gameTime);
            }
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            if (mCurrentScreen == mTitleScreen)
            {
                m_spriteBatch.Begin();
                mTitleScreen.Draw(m_spriteBatch);
                DrawCrosshair(m_spriteBatch);
                m_spriteBatch.End();
            }
            else if (mCurrentScreen == mGameScreen)
            {
                m_spriteBatch.Begin();
                mGameScreen.Draw(m_spriteBatch);
                m_spriteBatch.End();
            }
            else if (mCurrentScreen == mHowToScreen)
            {
                m_spriteBatch.Begin();
                mHowToScreen.Draw(m_spriteBatch);
                DrawCrosshair(m_spriteBatch);
                m_spriteBatch.End();
            }
            else if (mCurrentScreen == mScoreScreen)
            {
                m_spriteBatch.Begin();
                mScoreScreen.Draw(m_spriteBatch);
                DrawCrosshair(m_spriteBatch);
                m_spriteBatch.End();
            }
            else if (mCurrentScreen == mCreditsScreen)
            {
                m_spriteBatch.Begin();
                mCreditsScreen.Draw(m_spriteBatch);
                DrawCrosshair(m_spriteBatch);
                m_spriteBatch.End();
            }
            else if (mCurrentScreen == mDeathSplashScreen)
            {
                m_spriteBatch.Begin();
                mDeathSplashScreen.Draw(m_spriteBatch);
                DrawCrosshair(m_spriteBatch);
                m_spriteBatch.End();
            }
            else if (mCurrentScreen == mVictorySplashScreen)
            {
                m_spriteBatch.Begin();
                mVictorySplashScreen.Draw(m_spriteBatch);
                DrawCrosshair(m_spriteBatch);
                m_spriteBatch.End();
            }
            base.Draw(gameTime);
        }

        public void TitleScreenEvent(object obj, EventArgs e)
        {
            mouse_state = Mouse.GetState();
            MouseState curr_mouse = Mouse.GetState();
            while (mouse_state == curr_mouse)
            {
                curr_mouse = Mouse.GetState();
            }
            switch (TitleScreen.count)
            {
                case 0: mCurrentScreen = mGameScreen; break;
                case 1: mCurrentScreen = mHowToScreen; break;
                case 2: mCurrentScreen = mScoreScreen; break;
                case 3: mCurrentScreen = mCreditsScreen; break;
            }
            
        }

        public void GameScreenEvent(object obj, EventArgs e)
        {
            score = GameScreen.score;
            Enemy.ObjectList.Clear();
            Collectable.ObjectList.Clear();
            Projectile.ObjectList.Clear();
            mGameScreen = new GameScreen(this.Content, new EventHandler(GameScreenEvent));
            if (outcome == false)
            {
                mCurrentScreen = mDeathSplashScreen;
            }
            else if (outcome == true)
            {
                SplashScreen.text = "";
                mCurrentScreen = mVictorySplashScreen;
            }
        }

        public void HowToScreenEvent(object obj, EventArgs e)
        {
            mTitleScreen = new TitleScreen(this.Content, new EventHandler(TitleScreenEvent));
            mouse_state = Mouse.GetState();
            MouseState curr_mouse = Mouse.GetState();
            while (mouse_state == curr_mouse)
            {
                curr_mouse = Mouse.GetState();
            }
            mCurrentScreen = mTitleScreen;
        }
        public void ScoreScreenEvent(object obj, EventArgs e)
        {
            mTitleScreen = new TitleScreen(this.Content, new EventHandler(TitleScreenEvent));
            mouse_state = Mouse.GetState();
            MouseState curr_mouse = Mouse.GetState();
            while (mouse_state == curr_mouse)
            {
                curr_mouse = Mouse.GetState();
            }
            mCurrentScreen = mTitleScreen;
        }
        public void CreditsScreenEvent(object obj, EventArgs e)
        {
            
            mouse_state = Mouse.GetState();
            MouseState curr_mouse = Mouse.GetState();
            while (mouse_state == curr_mouse)
            {
                curr_mouse = Mouse.GetState();
            }
            if (CreditsScreen.count == 1)
            {
                mTitleScreen = new TitleScreen(this.Content, new EventHandler(TitleScreenEvent));
                mCurrentScreen = mTitleScreen;
            }
            else if (CreditsScreen.count == 2)
            {
                this.Exit();
            }
        }
        public void SplashScreenEvent(object obj, EventArgs e)
        {
            mouse_state = Mouse.GetState();
            MouseState curr_mouse = Mouse.GetState();
            while (mouse_state == curr_mouse)
            {
                curr_mouse = Mouse.GetState();
            }
            if (SplashScreen.count == 1)
            {
                SplashScreen.enterPressed = false;
                ScoreUpdater.newHighScore = false;
                SplashScreen.flag = false;
                mTitleScreen = new TitleScreen(this.Content, new EventHandler(TitleScreenEvent));
                mCurrentScreen = mTitleScreen;
            }
            else if (SplashScreen.count == 2)
            {
                this.Exit();
            }
        }
        private void DrawCrosshair(SpriteBatch batch)
        {
            MouseState mstate = Mouse.GetState();
            Vector2 mouseLoc = new Vector2(mstate.X - (CrosshairTexture.Width / 2),
                                           mstate.Y - (CrosshairTexture.Height / 2));
            batch.Draw(CrosshairTexture, mouseLoc, Color.Black);
        }
    }
}
